package com.sikaryofficial.common.core.valid;

import cn.hutool.core.text.CharSequenceUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义xss校验注解实现
 * 
 * @author ruoyi
 */
public class PostCodeValidator implements ConstraintValidator<PostCodeValid, String>
{
    /**
     * 仅包含数字时不予匹配
     */
    private static final String HTML_PATTERN = "[a-zA-Z0-9\\s]*$";

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext)
    {
        if (CharSequenceUtil.isBlank(value))
        {
            return true;
        }
        return containsRegChar(value);
    }

    private static boolean containsRegChar(String value)
    {
        Pattern pattern = Pattern.compile(HTML_PATTERN);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }
}